This question already has answers here:
How do I tell if a file does not exist in Bash?
(20 answers)
Closed 6 years ago.
I have a prompt in my shell script where the user can choose a directory. I am trying to make it so that if a .gz file exists in that directory, they exit the loop, and if not, the user is asked again to choose a directory, but it just isn't working. Here's what I have so far:
ls -d */
while :
do
echo "Which Directory do you want to access?"
read input_variable1
cd $input_variable1
if [ CHECK FOR .gz ]
then
break
else
ls -d */
echo "no .gz files to unzip. Try again."
fi
done
Test supports using wildcards, so [ -f *.gz ] will be true if there is one file matching *.gz (and false otherwise - as #tripleee pointed out, this will fail with multiple matches; see below for a better variant).
Another possibility is to use if ls *.gz &>/dev/null; then ...; else ...; fi (which will not throw any errors).
Related
This question already has answers here:
How to manually expand a special variable (ex: ~ tilde) in bash
(19 answers)
Closed last month.
I have the following variable that shows where I should write my report to
REPORT="ZFS(~/Q4Y22/report/q4y22.md)"
I have used sedto extract the path and dirname command to get the
~/Q4Y22/report/dir name which I assign to reportdir variable.
However when I run the following test, instead of the Q4Y22 dir being created in my home dir, it literally creates the following directory hierachy ~/Q4Y22/report inside my home for example farai/~/Q4Y22/report instead of farai/Q4Y22/report
if [ -d $reportdir ]
then
echo "dir exists, keep moving"
else
mkdir -p $reportdir
fi
How do I get around this, I tried using sed to remove the tilde from the path but i was wondering if there is a more clever way
My try on this:
#!/bin/bash
REPORT="~/Q4Y22/report/q4y22.md"
reportdir=$(dirname "${REPORT/'~'/$HOME}")
if [ -d "$reportdir" ]
then
echo "dir exists, keep moving"
else
mkdir -p "$reportdir"
fi
This is a bit fragile since you could have ~ in the filename, though, but you could improve this script to replace only the first char if it's a tilde.
This question already has answers here:
How to make a programme executable anywhere in the SHELL
(4 answers)
How do I run a shell script without using "sh" or "bash" commands?
(13 answers)
Closed 1 year ago.
I am learning shell scripting, and I have written two scripts one that copies many files at once to a folder and if the folder does not exist it creates it, works like a charm! You may use it :D and the other one that moves them, I mean cuts and pastes :)
to copy:
#!/bin/bash
if [ ! -e "$2" ]
then
mkdir $2
fi
if [ "$3" ]; then
cp -rn $1/*.$3 $2
echo "Copying *.$3 done!"
else
cp -rn $1/*.* $2
echo 'Copying done!'
fi
to move:
#!/bin/bash
if [ ! -e "$2" ]
then
mkdir $2
fi
if [ $3 ]; then
mv -i $1/*.$3 $2
echo "Moving *.$3 done!"
else
mv -i $1/*.* $2
echo 'Moving done!'
fi
I would like to be able to use them like any other shell command (eg. ls, ping, cd...) everywhere in my system. How can I achieve that?
Thanks!
You need to
Name each script file what you'd like to call it when you run it (without the extension), e.g. copymany and movemany
Place the copymany and movemany files in a folder, perhaps ~/bin
Add the folder to your $PATH environment variable, e.g. export PATH=$PATH:$HOME/bin, in your .bashrc or .zshrc
This question already has answers here:
How to loop over files in directory and change path and add suffix to filename
(6 answers)
Closed 3 years ago.
I am trying to loop through every file in a user specified directory.
Here's my code:
clear
echo "enter the directory path: \n"
read directory
for file in $directory; do
echo $file
done
My input, e.g.: /home/user/Downloads
Output I get: /home/user/Downloads
If I use
clear
for file in *; do
echo $file
done
It works, but it shows only the contenets of current directory
If you only want the files non-recursively in the current directory, combine what you have:
read -p 'Enter the directory path: ' directory
for file in "$directory"/*; do
echo "$file"
done
If you want to loop recursively and you have bash 4, it's not much harder:
shopt -s globstar
for file in "$directory"/**/*; do …
But if you only have bash 3, you'd be better off using find.
find "$directory"
Try
dir="${GOL_HOME}/test_dir"
file="file_*.csv"
for file in `cd ${dir};ls -1 ${file}` ;do
echo $file
done
You can write this script
#!/bin/bash
clear
echo "enter the directory path: \n"
read directory
for file in $directory/*; do
echo $file
done
This question already has answers here:
How to go to each directory and execute a command?
(12 answers)
Closed 5 years ago.
I'm currently trying to write a script which will cd to a directory and execute a python script if it matches a predefined string to the directory name. I need the code to change to a directory with the string 'batch' in it and execute the script. The most useful tool would probably be the case statement, but I can't get it to work for some reason. I'm fairly new to bash scripting, so i'm not really sure why this isn't working.
Here's my code:
#!/bin/bash
dir_string="batch"
workingdir=$PWD
for dir in *;do
echo "$dir"
if [[-d "$dir"]]; then
case "$dir_string" in
$dir)
cd $workingdir/$dir
for i in *;do
execute python script
done
;;
*)
echo "Can't find appropriate directories."
;;
esac
fi
done
Any help is appreciated!
What you are trying to achieve with your script, can be achieve with find:
#!/bin/bash
dir_string="batch"
workingdir=$PWD
for dir in $(find "$workingdir" -type d|grep $dir_string)
do
(cd "$dir" && execute python script)
done
src_dir="/export/home/destination"
list_file="client_list_file.txt"
file=".csv"
echo "src directory="$src_dir
echo "list_file="$list_file
echo "file="$file
cd /export/home/destination
touch $list_file
x=`ls *$file | sort >$list_file`
if [ -s $list_file ]
then
echo "List File is available, archiving now"
y=`tar -cvf mystuff.tar $list_file`
else
echo "List File is not available"
fi
The above script is working fine and it's supposed to create a list file of all .csv files and tar's it.
However I am trying to do it from a different directory while running the script, so it should go to the destination directory and makes a list file with all the .csv in destination directory and make a .tar from the list file(i.e archive the list file)
So i am not sure what to change
there are a lot of tricks in filename handling. the one thing you should know is file naming under POSIX sucks. commands like ls or find may not return the expected result(but 99% of the time they will). so here is what you have to do to get the list of files truely:
for file in $src_dir/*.csv; do
echo `basename $file` >> $src_dir/$list_file
done
tar cvf $src_dir/mystuff.tar $src_dir/$list_file
maybe you should learn bash in a serious manner and try to google first before you asking question in SO next time.
http://www.gnu.org/software/bash/manual/html_node/index.html#SEC_Contents
http://tldp.org/HOWTO/Bash-Prog-Intro-HOWTO.html