Bash script

I want a script that change to a directory (the first param) and display all the files inside it

Why this really simple script don't change directory ?

#!/usr/bin/env bash
cd $1
dates=`ls`
echo $dates

Worked for me. Try running the script in debug mode to see what's happening.

Either add this line to your script:

set -x

Or run the script like so:

sh -x <script>

Hello,

This

 $1 

in your script means the 1st argument that is passed to your script while running, so are you passing any argument while running the script?

Also please mention if you are passsing absolute path or relatvie path here.

Thanks,
R. Singh

yes, I pass the relative folder to cd.
I call the script in this way:

./bashScriptTest.sh data

So I whant the script cd to the inner directory "data".

The script fail and give me this error:

No such file or directory: cd: data

PS: I forgot to say that I'm testing this scrpt on CYGWIN, but it will run on a UNIX machine when I will finished it (it will become more complex)

yeah that's same I was referring in previous post, could you please do a

pwd

command and try to pass the same while runing script. It should work.
Get back to me if you need any furter queries.

Thanks,
R. Singh

I'm not sure what was your question. That is what I've done:

#!/usr/bin/env bash
echo $(pwd)
cd $1
dates=`ls`
echo $dates

And the result is:

$ ./bashScriptTest.sh data
/home/Admin
: No such file or directory: cd: data
bashScriptTest.sh test.txt

/home/Admin is the correct pwd.

No, I want to you to put the absolute path for date. Let me give you an example here for same.

 
 
$ cat ls_checking.ksh
cd $1
dates=`ls`
echo $dates

$ ksh ls_checking.ksh /user/archsupp/bin/singh_testing/awk_programming

 

Output is as follows.

$ ksh ls_checking.ksh /home/user/bin/singh_testing/awk_programming
11_char_example 1stfile 200005051111 2nd 3rdfile E_to_e.ksh without_length_70_lines

Here is the example while running script you should give the Date's abosolute path where Dat is present. Try the same and let me know please.

Thanks,
R. Singh

it's the same result:

Admin@L-IT-03404 ~
$ pwd
/home/Admin

Admin@L-IT-03404 ~
$ ./bashScriptTest.sh /home/Admin/data
: No such file or directory: cd: /home/Admin/data
bashScriptTest.sh data test.txt

Admin@L-IT-03404 ~

hey just try this if it works

#!/usr/bin/env bash
PWD=`pwd`
echo $PWD
cd $PWD/$1
dates=`ls`
echo $dates

Could you please let me know where that Data directory is present in your box.

Thanks,
R. Singh

/home/Admin/data

and this is the content of Admin :

bashScriptTest.sh  data  test.txt

Ahhhh.... got it, you should give as follows.

./bashScriptTest.sh /home/Admin

As simple as that there is no data directory presnet in /home/Admin so that only we are not able to run it. In case you want to see that data file content then you should make appropriate changes to it.

Thanks,
R. Singh

try running your script like

sh bashScriptTest.sh data

in Admin there is a data directory, The Admin content is in my previuos post.

The problem is that if I write the same command of the script directly in the shell, this commands works.
So if in the shell I wrote:
cd data
ls
The result is correct.
Instead running the script it does't change the directory and list the file in the current directory

---------- Post updated at 04:55 PM ---------- Previous update was at 04:54 PM ----------

it still don't work:

Admin@L-IT-03404 ~
$ sh bashScriptTest.sh data
: No such file or directorycd: data
bashScriptTest.sh data test.txt

can u post the script, the directory from which u r running your script and the output please?? post these 3 things if u can..

./bashScriptTest.sh /home/Admin/data

Could you please try the same. if it doesn't work please share the output of

 ls -ltr 

in Admin directory.

Thanks,
R. Singh

Result calling it with /home/Admin/data:

Admin@L-IT-03404 ~
$ ./bashScriptTest.sh /home/Admin/data
: No such file or directory: cd: /home/Admin/data
bashScriptTest.sh data test.txt

Result of ls -ltr

Admin@L-IT-03404 ~
$ ls -ltr
totale 9
-rwxr-xr-x  1 Admin None 29 22 lug 10.52 test.txt
drwxr-xr-x+ 1 Admin None  0 24 lug 11.19 data
-rwxr-xr-x  1 Admin None 51 24 lug 16.04 bashScriptTest.sh

---------- Post updated at 05:08 PM ---------- Previous update was at 05:06 PM ----------

This is the script:

#!/usr/bin/env bash
cd $1
dates=`ls`
echo $dates

The directory where I call the script is: /home/Admin

I call the scrpt passing: data.

So I expect that my script change directory to the inner directory data and that list le files

Hello,

drwxr-xr-x+ 1 Admin None 0 24 lug 11.19 data

The plus sign is an indication of ACL (access control list) which is an extension to the normal POSIX permissions system which increases security by allowing the system more fine-tuning to a access for a file/folder in system. The ACL's can be implemented using setfacl command, we will see about ACL in our coming posts. Please tune in for more stuff on this Permissions.

So it may not be giving you permissions to check contents. Please check your script for other paths it should work fine.

Thanks,
R. Singh

I suspect you have DOS newline characters in your script which is causing it to behave incorrectly. Can you give the output of:

cat -vet bashScriptTest.sh
Admin@L-IT-03404 ~
$ cat -vet bashScriptTest.sh
#!/usr/bin/env bash^M$
cd $1^M$
dates=`ls`^M$
echo $dates