我编写了一个 bash 脚本,它创建一系列目录并将项目克隆到选定的目录。
为此,我需要到cd每个目录(project 1和project 2),但脚本不会cd到第二个目录,也不会执行命令。
相反,它会cd在project2目录中克隆后停止。为什么不调用cd_project1下面代码中的函数?
#!/bin/bash
#Get the current user name
function my_user_name() {
current_user=$USER
echo " Current user is $current_user"
}
#Creating useful directories
function create_useful_directories() {
if [[ ! -d "$scratch" ]]; then
echo "creating relevant directory"
mkdir -p /home/"$current_user"/Downloads/scratch/"$current_user"/project1/project2
else
echo "scratch directory already exists"
:
fi
}
#Going to project2 and cloning
function cd_project2() {
cd /home/"$current_user"/Downloads/scratch/"$current_user"/project1/project2 &&
git clone …Run Code Online (Sandbox Code Playgroud) 我编写了一个小的 bash 脚本来查找名为anaconda或miniconda在我的用户中的目录$HOME。但是它miniconda2在我家中找不到该目录。
我怎么能解决这个问题?
if [ -d "$HOME"/"(ana|mini)conda[0-9]?" ]; then
echo "miniconda directory is found in your $HOME"
else
echo "anaconda/miniconda is not found in your $HOME"
fi
Run Code Online (Sandbox Code Playgroud)
PS:如果我有[ -d "$HOME"/miniconda2 ]; then,那么它会找到 miniconda2 目录,所以我认为错误在于部分"(ana|mini)conda[0-9]?"
我希望脚本是通用的。对我来说,它是 miniconda2,但对于其他一些用户,它可能是 anaconda2、miniconda3 等等。
我正在运行一个我想检查是否miniconda已经安装的软件。因此,我.bashrc使用grep命令检查了文件中的“miniconda”或“anaconda”字符串。但是,它还会找到在.bashrc文件中注释掉的上述任何一个字符串,这是我不想要的。我该如何解决?我bashscript长相的相关部分如下。
#Finding if miniconda or anaconda string is in bashrc
if grep -qF -e miniconda -e anaconda "$HOME"/.bashrc ;then
echo "miniconda is found in .bashrc"
Run Code Online (Sandbox Code Playgroud)
我已经通过在.bashrc文件中添加以下几行来测试这一点。
#anaconda
#miniconda
Run Code Online (Sandbox Code Playgroud)
终端输出
jen@scs400:/scratch$ source bash_script.sh
miniconda is found in .bashrc
Run Code Online (Sandbox Code Playgroud) 我的目录中有很多*.c文件。*.h我想将所有这些文件转换为Linux兼容的格式。我尝试执行以下脚本并且它运行但没有任何转换。
我还需要检查所有内容是否都已成功转换。因此,我对其输出进行过滤并将其与目录中的原始文件进行比较。
我该如何解决它?
#!/bin/bash
function converting_files() {
cd "/path/to/dir" && find . -type f -print0 | xargs -0 | dos2unix
}
function success_of_converting_files() {
FILES=colsim_xfig/xfig.3.2.5c/*
#There are 250 files in the dir and not all but most of them are .c and .h
for i in {000..249} ; do
for f in "$Files" ; do
#I think *.txt line ending is fine as we only want to compare
dos2unix < myfile{i}.txt | cmp -s …Run Code Online (Sandbox Code Playgroud)