I have a simple code which is:
#!/bin/bash
#LaTex code generator for figures.
ls *.pdf > pdfs.file
ls *.ps > ps.file
pwd=$(pwd)
for i in {1..2}
do
# var=$(awk 'NR==$i' 'pdfs.file')
echo $pwd
echo $pwd > testfile
done
Why aren't the commands in the for loop working?
The $pwd isnt echoed neither is the testfile created.
I tried these commands without the for loop in a terminal and they work fine.
My bash file is made executable by chmod +x bashfile.sh
What I am trying to do is this:
Find pdfs or eps files and populate pdfs.file and eps.file with their file names.
Step through row by row and grab these file names and append to $pwd.
Then append $pwd$var to the include graphics command in latex.
I'm not sure what you're doing wrong, but this works fine for me:
for i in {1..2}; do
echo $PWD
echo $PWD > /tmp/testfile
done
echo "File contents: $(cat /tmp/testfile)"
This successfully returns the following:
/tmp
/tmp
File contents: /tmp
Did you write the bash file using a Windows editor? Maybe you have a problem with line terminators. Try dos2unix bashfile.sh.
Related
I am writing a .sh file to print the file names one by one. I have installed ubuntu in windows 10 and using the windows command prompt for executing below code. It is returning "E:/Official/Backups/GGG/*" instead of file names inside. I have also changed the EOL conversion to Unix(LF) by using notepad ++. please help.
#!/bin/bash
folder="E:/Official/Backups/GGG"
for entry in "$folder"/*
do
echo "$entry"
done
Running the script outputs:
$ bash test1.sh
E:/Official/Backups/GGG/*
Output of echo $-
himBHs
Output of ls -ld E:/Official/Backups/GGG
ls: cannot access 'E:/Official/Backups/GGG': No such file or directory
My bash in WSL does not recognize windows paths. If I want to access E:\Official\Backups\GGG I would have to use /mnt/e/Official/Backups/GGG.
I assume, the same goes for your WSL bash. Therefore the "path" E:/Official/Backups/GGG is just a non-existing directory and your observed behavior is to be expected. With bash's default settings a * just stays there as a literal if the directory does not exist or is empty. Example:
$ echo /dir/that/doesnt/exist/*
/dir/that/doesnt/exist/*
$ echo /dir/that/exists/but/is/empty/*
/dir/that/exists/but/is/empty/*
$ echo /dir/*
/dir/file1 /dir/file2 /dir/file3 ...
GGG folder is not exists. Please check and update with valid folder and try again.
#!/bin/bash
folder="E:"
for entry in "$folder"/*
do
echo "$entry"
done
I have a js script that converts kml location history files to csv. I wrote a bash script to loop through all the files in a directory. The script works when I execute it from command line ./index.js filename.kml > filename.csv
But nothing happens when I execute the bash file that is supposed to loop through all files.
I know it probably is a simple mistake but I can't spot it.
#!/bin/bash
# file: foo.sh
for f in *.kml; do
test -e "${f%.kml}" && continue
./index.js "$f" > "-fcsv"
done
Just delete the "&& continue", if I'm not wrong you're skipping the current iteration with the "continue" keyword, that's why nothing happens
EDIT
Also, you shouldn't test if the file exists, the for loop is enough to be sure that "f" will be a valid .kml file. Anyways, if you still want to do it you have to do it like:
#!/bin/bash
# file: foo.sh
for f in *.kml; do
if [ -e "$f" ]; then
./index.js "$f" > "$f.csv"
fi;
done
Name of a script - backup_script.sh
Location of a script on server - /home/company_folder/company_site_backups
Line added to the cron file:
#monthly /home/company_folder/company_site_backups/backup_script.sh
#!/bin/bash
DIR="/home/company_folder/company_applications/*"
BACKUPDIR="/home/company_folder/company_site_backups"
NOW=`date +\%Y\%m\%d`
cd $DIR
for i in $DIR; do zip -r "${i%/}.zip" "$BACKUPDIR/$i-$NOW"; done
ls -l
echo "Done!"
But unfortunately my script does not work properly. Actually. It does not run at all! I do not see any errors in the syntax.
Does anyone know how to fix it?
The cd $DIR seems strange; if the first entry found by /home/company_folder/company_applications/* is a directory it will change to that directory; if it is a file (or company_applications is empty) it will get an error.
Perhaps everything is running correctly except that because of the above your ls -l is not running in the directory you expect? Try removing the cd and changing it to ls -l $DIR.
It also seems very strange to me that you are zipping up content from a backup directory into an applications directory. Perhaps you meant to be doing:
zip -r "$BACKUPDIR/`basename $i`-$NOW" $i
could you try this;
#!/bin/bash
DIR="/home/company_folder/company_applications/*"
BACKUPDIR="/home/company_folder/company_site_backups"
NOW=`date +\%Y\%m\%d`
cd $DIR
for i in $DIR
do
base=$(basename "$i")
zip -r $BACKUPDIR/$base-${NOW}.zip $i
done
ls -l $BACKUPDIR
echo "Done!"
--Bash 4.1.17 (running with Cygwin)
Hello, I am trying to pass the date into the --suffix option on the move (mv) command. I am able to pass in a simple string (like my name) but unable to pass in the date. If you run the script below you will see that the mv command with the suffix="$var" works but suffix="$now" does not.
#!/bin/bash
dir="your directory goes here"
now="$(date "+%m/%d/%y")"
var="_CARL!!!"
echo "$now"
echo "$var"
cd "$dir"
touch test.txt
# error if already exists
mkdir ./stack_question
touch ./stack_question/test.txt
mv -b --suffix="$var" test.txt ./stack_question/
The idea is that if test.txt already exists when trying to move the file, the file will have a suffix appended to it. So if you run this script with:
--suffix="$var"
you will see that the stack_question directory contains two files:
test.txt & test.txt_CARL!!!
But, if you run this script with:
--suffix="$now"
you will see that in the stack_question directory only contains:
test.txt
Any help on this would be greatly appreciated!
It is because you have embedded / in your date format try
now="$(date +%m_%d_%y)"
I have 2 lines of code
1) With the following code:
for i in `ls *.properties`; do cat $i; done
I get the error:
cat: file_name.properties: No such file or directory.
2) On the other hand:
for i in *.properties; do cat $i; done
Works fine!
I thought both were the same. Can someone help me understand the difference between the two? Using bash shell.
What does the following command print?
cat -v <<< `ls *.properties`
I guess the problem is, that ls is a strange alias, e.g. something like
ls='ls --color'
Edit: I have seen this before. The alias should be: alias ls='ls --color=auto'
Most probably there is a directory which matches *.properties. Then ls will output the files in this directory without the directory name. Then the cat will not find the given filename.
So please check, whether file_name.properties is in the actual directory or in some subdirectory.
EDIT
To reproduce the problem you can try this:
# cd /tmp
# mkdir foo.properties
# touch foo.properties/file_name.properties
# for i in `ls *.properties`; do cat $i; done
cat: file_name.properties: No such file or directory