.sh file returned file path instead of file name - bash

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

Related

pass long file argument from batch script to bash script in windows with cygwin

I am following this post:
http://blog.dotsmart.net/2011/01/27/executing-cygwin-bash-scripts-on-windows/
I need to execute a batch file with an argument and pass this argument to a bash script. The argument is a long file name with spaces, something like FILE WITH SPACES.xlsx.
In Windows:
c> program.cmd "FILE WITH SPACES.xlsx" # Windows adds the quotes to complete the name of the file.
The batch script must pass the name of the file "FILE WITH SPACES.xlsx" to a .sh script, something like this:
# program.sh "FILE WITH SPACES.xlsx"
Here is the problem, the file is passed without the quotes:
# program.sh FILE WITH SPACES.xlsx
The script program.sh has:
#!/bin/sh
anyprogram "$1"
The quotes are not passed and the program anyprogram doesn't locate the file FILE WITH SPACES.xlsx.
i tried escaping the " with \ but not work:
#!/bin/sh
anyprogram "\"$1"\"
Also, with:
#!/bin/sh
anyprogram '"$1"'
None looks to work, any suggestion?
Batch files can be tricky with the way they handle double-quotes, and adding Bash variables can make things even trickier. Here is a solution that I believe solves your specific issue:
Batch file location: C:\Users\Jamey\Desktop\blah.bat
Batch file contents:
#ECHO OFF
ECHO I AM A BATCH FILE.
CD C:\Users\Jamey\Desktop
ECHO TESTING > %1
TYPE %1
ECHO.
SET scriptpath=/cygdrive/c/users/jamey/desktop
C:\cygwin64\bin\bash.exe -c "$scriptpath/program.sh "%1""
Bash script location: C:\Users\Jamey\Desktop\program.sh
Bash script contents:
#!/bin/bash
echo "I AM A BASH SCRIPT."
echo "A WINDOWS BATCH FILE TOLD ME TO SAY THIS: $1"
echo
ls -l "$1"
echo
cat "$1"
echo
rm -v "$1"
Here is the cmd.exe output from running blah.bat "FILE NAME WITH SPACES.txt":
C:\Users\Jamey\Desktop>blah.bat "FILE NAME WITH SPACES.txt"
I AM A BATCH FILE.
TESTING
I AM A BASH SCRIPT.
A WINDOWS BATCH FILE TOLD ME TO SAY THIS: FILE NAME WITH SPACES.txt
-rwxrwx---+ 1 Jamey None 10 Sep 24 09:05 FILE NAME WITH SPACES.txt
TESTING
removed 'FILE NAME WITH SPACES.txt'
Alternately, if you replaced 'rm -v' with 'cygstart', "FILE NAME WITH SPACES.txt" would open in Notepad. Same goes for Excel with .xlsx extensions. I hope this answers your question!

Bash: passing a variable to mv command option

--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)"

Shell Scripting - Must not generate extra messages and its not but says I am

There is a similar question about this issue. But not the same solution.
I am to create a shell script that takes two parameters:
1.the desired file extension
2.the name of a single file to be renamed with that extension
The script should rename the file with the desired file extension. If the file does not exist, it should print out "fileName: No such file". It is producing this message but the professor's tests says it is producing unexpected messages(extra messages) but it is not. My shell script is:
#!/bin/sh
fileExtension="$1"
shift
oldName="${#}"
extension=${oldName##*.}
if test -r "$oldName"
then
if "$fileExtension" == $oldName.*
then
echo "$oldName"
else
newName="${oldName%.*}.$fileExtension"
mv "$oldName" "$newName"
fi
else
echo "$oldName": No such file
fi
Everytime I test it, it produces "fileName: no such file" and nothing else.
The test is executed by
./chExt2.sh cpp aardvark.CPP
where aardvark.CPP is not on the directory.
Any help or guidance would be much appreciated. Thank you
Your shebang is telling your shell to use /bin/sh to run the script. /bin/sh is typically a symlink to the real/default shell on the host. For example, mine's bash:
$ ls -l /bin/sh
lrwxrwxrwx 1 root root 4 Mar 27 2009 /bin/sh -> bash
It sounds like your professor's computer's /bin/sh is using a different shell than you are [expecting]. This script runs fine in ksh or bash, for example, but produces "extra output" if /bin/sh is tcsh:
fileExtension=cpp: Command not found.
Illegal variable name.

Script won't recognize the file / directory

For class we have to work on a remote server that the school hosts. So far I have made a lot of files on the server and I would like to back them up in case I want to transfer them to my laptop or in case I accidentally delete a directory or make a silly error. I found a tutorial and a script to back up the file and I decided to modify it so that it would determine what directory it's in (which will be the main user's) and the cd to the Documents. It also creates the directory Backups if it doesn't exist. I am still pretty new to this sort of scripting and any additional advice or post links would be greatly appreciated.
Code:
#!/bin/bash
#######################################################
## Simple backup script..
## Created by Matthew Brunt: (openblue555#gmail.com)
## Licensed under GNU GPL v3 or later, at your option.
## http://www.gnu.org/licenses/gpl.html
##
## Further edited by Michael Garrison to backup the
## directory it is located in and print the contents.
#######################################################
mkdir -p Backup
#Defines our output file
OUTPUT= $( cd Backup && pwd )/backup_$(date +%Y%m%d).tar.gz
#Defines our directory to backup
BUDIR=$( cd Desktop && pwd )
#Display message about starting the backup
echo "Starting backup of directory $BUDIR to file $OUTPUT"
#Start the backup
tar -cZf $OUTPUT $BUDIR
#Checking the status of the last process:
if [ $? == 0 ]; then
#Display confirmation message
echo "The file:"
echo $OUTPUT
echo "was created as a backup for:"
echo $BUDIR
echo ""
echo "Items that were backed up include:"
for i in $BUDIR; do
echo $i
done
echo ""
else
#Display error message message
echo "There was a problem creating:"
echo $OUTPUT
echo "as a backup for:"
echo $BUDIR
fi
I know that the original script works and it worked until I changed the $OUTPUT variable. I currently get the following result:
./backup.sh
./backup.sh: line 15: /Users/mgarrison93/Backup/backup_20121004.tar.gz: No such file
or directory
Starting backup of directory /Users/mgarrison93/Desktop to file
tar: no files or directories specified
There was a problem creating:
as a backup for:
/Users/mgarrison93/Desktop
I can see that it is not accepting the file name, but I don't know how to correct this.
I just tried changing $OUTPUT to /Backups/file-name.tar.gz which I originally had and it works fine. The problem seems to be $( cd Backup && pwd )/backup_$(date +%Y%m%d).tar.gz. Just not sure what is wrong.
Consider these two entirely different pieces of bash syntax: first, you have the syntax for setting a variable to a value permanently (in the current script),
<variable>=<value>
and then there is the syntax for running a command with a variable temporarily set to a value ,
<variable>=<value> <command> <argument> ...
The difference between these two is the space. After the =, once bash runs into an unquoted space, it takes that to mean that the <value> has ended, and anything after it is interpreted as the <command>.
In this line of your script,
OUTPUT= $( cd Backup && pwd )/backup_$(date +%Y%m%d).tar.gz
you have a space after OUTPUT=. bash interprets that to mean that OUTPUT is to be (temporarily) set to the empty string, and the rest of the line, i.e. the result of $( cd Backup && pwd )/backup_$(date +%Y%m%d).tar.gz, is a command and arguments to be run while OUTPUT is equal to the empty string.
The solution is to remove the space. That way bash will know that you're trying to assign the rest of the line as a value to the variable.

Why aren't the BASH commands in for loop working

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.

Resources