Tag Info

Hot answers tagged

11

every beginning is hard: #!/bin/bash read INPUT echo $INPUT len=$(echo -n "$INPUT" | LC_ALL=C.UTF-8 wc -m) echo $len specifically, there must not be a space surrounding = and a separate command needs to be enclosed inside $(...). Also, you might want to write your variables in quotes " using this syntax "${INPUT}", this ensures that the variable is not ...


10

You do: cat "$wd/wakeUp_blacklist.txt" | while read black; do In Bash: Each command in a pipeline is executed in its own subshell A subshell is another instance of bash, with its own state. That means that any variable changes you make on the right-hand side of a | conceptually "belong" to a different bash instance — the existing values and ...


9

If your system has the perl-based rename command you could do something like rename -- 's/(\d+)-(\d+)/sprintf("%d-%03d",$1,$2)/e' *.jpg Testing it using the -v (verbose) and -n (no-op) options: $ rename -vn -- 's/(\d+)-(\d+)/sprintf("%d-%03d",$1,$2)/e' *.jpg 0-10.jpg renamed as 0-010.jpg 0-19.jpg renamed as 0-019.jpg 0-1.jpg renamed as 0-001.jpg


6

I think preferably would be to use len=${#INPUT_STRING} as otherwise the end of line character added by echo will be counted in as well. Unless that is what you want.


6

Assuming you have your desired files in a text file, you could do something like while IFS= read -r file; do echo mkdir -p ${file%/*}; cp /source/"$file" /target/${file%/*}/${file##*/}; done < files.txt That will read each line of your list, extract the directory and the file name, create the directory and copy the file. You will need to ...


4

You can pipe it to stdin of the script: echo 2.25.9847261 | ./sendExamToRepo.sh Or redirect stdin to come from a file: ./sendExamToRepo.sh < SUID.txt


4

pax Pax is the best tool for this task. It's POSIX's replacement for cpio and tar (and unlike tar it includes a pass-through mode, not just archive creation and extraction). Sadly, it's omitted from the default installation in some Linux distributions, but it's only an apt-get/yum/emerge/… invocation away. Pax has a limitation: recently modified ...


3

my-program > >(pv -trabcN stdout > stdout) 2> >(pv -trabcN stderr > stderr) Would give you a progress like: stderr: 123MiB 0:00:03 [42.6MiB/s] [41.1MiB/s] stdout: 138MiB 0:00:03 [54.2MiB/s] [46.2MiB/s] (current (-r) and average (-a) speed. -a is relatively recent, you can omit it if your version of pv doesn't have it).


3

There seems to be an 'in' missing in the line with the case statement as well as a closing esac: echo "Do you wish to search again? [y/n]" read INPUT_STRING2 case $INPUT_STRING2 in y) ;; *) exit ;; esac


3

You have set your PATH variable to /sample/data. The previous contents of the PATH variable have been overwritten with this. As a result, your script looks in /sample/data for scp and sshpass, fails to find them there, and gives you the error messages that you are seeing. Try changing the PATH variable name to a different name, e.g: ...


3

Run the script either as: bash script.sh or just: ./script.sh When bash is run using the name sh, it disables most of its extensions, such as the [[ testing operator. Since you have the #!/bin/bash shebang line, you don't need to specify the shell interpreter explicitly on the command line. Running the script as a command will use that line to find ...


3

If you have zsh shell, you could do something like below. zmv '([0-9])-([0-9]##).(jpg)' '$1-${(l:3::0:)2}.$3' Testing touch 0-1.jpg touch 0-23.jpg touch 0-345.jpg touch 0-6.jpg touch 0-05.jpg Change the shell to zsh and if zmv is not loaded, you could do autoload zmv. Now, you could add -n flag to the zmv command to see what will happen if you ...


3

You should be using menu not message box. Try this script: #!/bin/bash let i=0 # define counting variable W=() # define working array while read -r line; do # process file by file let i=$i+1 W+=($i "$line") done < <( ls -1 /home ) FILE=$(dialog --title "List file of directory /home" --menu "Chose one" 24 80 17 "${W[@]}" 3>&2 2>&1 ...


3

Okay, I feel like you might be asking one of two questions, so I will try to answer both. What libraries can one use to create ncurses like interfaces for shell scripts? Actually, I would never have recommended ncurses directly for shell scripts anyway since it's really not meant to be used by shell languages. Instead, I would recommend dialog. Dialog is ...


3

You did two syntax errors (in after the variable and esac as end of the case statement): echo "Do you wish to search again? [y/n]" read INPUT_STRING2 case $INPUT_STRING2 in "y") echo "Searching again" ;; *) exit ;; esac


2

This is a companion program for less. It is used internally in some configurations (determined at compile time) to call a shell from commands where you can specify a wildcard pattern that stands for a list of file names: the “examine” (:e) command and a few others (-o, -O, -T). The weird quoting is something that less parses internally. The point of using ...


2

You can add some shell scripting to /etc/bashrc or /etc/bash.bashrc maybe bepending on your linux distribution. Those are executed when user logs in remotely via SSH. Just test if there is $SSH_CLENT variable to distinguish the ssh login. There will be other usefull variables for your needs, like: SSH_ASKPASS=/usr/lib/ssh/x11-ssh-askpass ...


2

The idea is to archive the file, pipe the archive to the other side, which unarchives its standard input. Something like: tar cf - /path/to/file_in_dom0 | qvm-run --pass-io <dst_domain> 'cd / && tar xf -' This will preserve the full pathname. If you do not want that, you can do something similar in a script, and use the dirname, basename and ...


2

The shell script will automatically be the environment variable of the user executing the script. You might consider more importantly how to specify extra variables for your scripts. To add to the default path of the user running the script you would add a line such as: PATH="$PATH:/newAddedPath" That will add to the path the area you want included. ...


2

result=$(grub-md5-crypt | grep xy) echo $result If grub-md5-crypt prints to stderr use: result=$(grub-md5-crypt 2>&1 | grep xy) echo $result


2

You could use find with rsync to do this. find ./source/ -newer /tmp/foo -print0 | rsync -av --files-from=- --from0 ./ ./destination/ Testing I created 2 directories named source and destination. For testing purposes, I created a temporary file with a timestamp as below. touch --date "2014-09-08" /tmp/foo Now, inside source directory, I created ...


2

another solution in case you do not have "rename: for file in *.jpg; do [ -f "$file" ] || continue #skips if no jpg file present in current dir echo "${file}" | awk -F'[-.]' ' { new=sprintf("%03d",$2); print "echo TESTING mv ", $0," ",$1"-"new"."$3 }' done | bash (edit: moved the | bash so it's only invoked once, and not ...


1

If you want the entire script to run as another user, my usual technique for doing this is adding something similar to the following to the very top of the script: target_user="foo" if [ "$(whoami)" != "$target_user" ]; then exec sudo -u "$target_user" -- "$0" "$@" fi Note that I use sudo here and not su. su makes it stupidly difficult to pass arguments ...


1

If all the matching files are in the current directory (and not in any subdirectory or if the subdirectory names do not contain -), you can use for step 1 to 3: find -regex '.*/[0-9]+-[0-9]+-[0-9]+-NEW\.XML' | sort --field-separator=- --key=2 > filelist and for step 4: while IFS= read -r line; do cp -v $line /PATH/TO/DESTFOLDER/ done < filelist ...


1

Another answer, a little simpler than the others: #!/bin/bash fields=$(sed -r -e 's/-1/ /g' -e 's/,/ FS /g' \ -e 's/([0-9]+)/\$\1/g' control_file.txt) awk -F, "{print ${fields}}" $1 The first command converts control_file.txt into a suitable awk command: $1 FS $3 FS $5 FS FS $8 FS FS $4 to run it: $ ./script.sh input.csv col1,col3,col5,,col8,,col4 ...


1

If it doesn't work in your shell script you might want to use bash. Just add: #!/bin/bash It must be in the first line of your file! This means your script will be using bash interpreter other than normal shell's one (/bin/sh). Completing noEntry's answer, you can also save output to a file. grub-md5-crypt | grep xy > output Or: ...


1

It seems that your clamscan didn't generate any log file on output. Just change clamscan blah blah >/dev/null 2>/dev/null to clamscan blah blah &>/tmp/scan.log and check the scan.log - there probably some hints.


1

Here's another approach. Since you only need to rename a specific subset of files and since you already know what those are, just list them and rename them: for i in {1..99}; do mv -- 0-$i.jpg 0-$(printf '%03d\n' $i).jpg; done That will list the numbers from 1 to 99, saving each value as $i. You don't care about the rest since those are already named ...


1

There is no guarantee that there is a subdirectory in the tar file, or that there is only one. I have a special script to extract tar archives that first creates a temporary directory, extracts the file in that directory, and then counts the number of entries in that directory and if it is only one directory/file it moves it up from the temporary ...


1

use a code like this : java YourApp "$1" And run your code like this : ./app.sh your_argument



Only top voted, non community-wiki answers of a minimum length are eligible